import {
  getVipTypes,
  buyVipGoods
} from '../../api/user'
// components/vip/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    list: [],
    vip: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    open() {
      this.setData({
        show: true
      })
      this.init()
    },
    init() {
      getVipTypes({
        pageIndex: 1,
        state: 1,
        type: 0
      }).then(res => {
        console.log(res)
        if (res && typeof res == 'object') {
          this.setData({
            list: res
          })
        }
      })
    },
    handleClick(e) {
      console.log(e)
      this.setData({
        vip: e.currentTarget.dataset.item
      })
    },
    handleClose() {
      this.setData({
        show: false
      })
    },
    handleOpenVip() {
      if(!this.data.vip){
        wx.showToast({
          title: '请选择',
          icon:'none'
        })
        return 
      }
      var app = getApp()
      console.log(app.globalData.ip)
      buyVipGoods({
        subjectId: this.data.vip.goodsId,
        payType: 30,
        type: 2,
        clientIp: app.globalData.ip,
        isQr: 4,
        couponCodes: "",
      }).then(res => {
        if (res) {
          const _this = this
          wx.requestPayment({
            nonceStr: res.nonce_str,
            package: res.package,
            signType: 'MD5',
            paySign: res.sign,
            timeStamp: res.timestamp,
            success(res) {
              console.log(res)
              wx.showToast({
                title: '支付成功,请退出重新登录',
                icon: 'success'
              })
            },
            fail(err) {
              console.log(err)
              wx.showToast({
                title: '支付失败',
                icon: 'error'
              })
            }
          })
        }
      })
    }
  }
})